#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > cc_rsp_gammafreq.info <<'%EOF%'
   cc_rsp_gammafreq
   ----------------
   Molecule:         HF in C2
   Wave Function:    CCS / 6-31G**
   Test Purpose:     Second hyperpolarizability (cubic response):
                     - Parallel isotropic average (gamma_|| ) 
                     - ESHG & THG for one frequency
                     - No use of special symmetries 
%EOF%

#######################################################################
#  INTEGRAL INPUT
#######################################################################
cat > cc_rsp_gammafreq.mol <<'%EOF%'
BASIS
6-31G**
Hydrogen flourid
small test basis
    2    1  Y  X      1.00D-15
        9.0   1    
F     0.0000000000000000  0.0000000000000000 1.76320000000000000      
        1.0   1
H     0.0000000000000000  0.0000000000000000 0.00000000000000000      
%EOF%

#######################################################################
#  DALTON INPUT
#######################################################################
cat > cc_rsp_gammafreq.dal <<'%EOF%'
**DALTON INPUT
.RUN WAVE FUNCTIONS
**INTEGRAL
.DIPLEN
**WAVE FUNCTIONS
.TITLE
  second hyperpolarizability for the HF molecule
  for a CCS wavefunction compute:
  - parallel isotropic average (gamma_|| ) 
  - ESHG & THG for one frequency
  - no use of special symmetries 
.CC
*SCF INPUT 
.THRESH
 1.0d-12
*CC INPUT 
.CCS
.THRENR
 1.0d-8
.THRLEQ
 1.0d-8
*CCQR
.AVERAGE
HYPERPOLARIZABILITY
.STATIC
.SHGFREQ
   1
 0.04282270  
*CCCR
.AVERAGE
GAMMA_PAR            
GENERIC
.STATIC
.ESHGFR
   1
 0.04282270  
.THGFRE
   1
 0.04282270  
**END OF DALTON INPUT
%EOF%
#######################################################################

 

#######################################################################

#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >cc_rsp_gammafreq.check
cat >>cc_rsp_gammafreq.check <<'%EOF%'
log=$1

if [ `uname` = Linux ]; then
   GREP="egrep -a"
else
   GREP="egrep"
fi

if $GREP -q "not implemented for parallel calculations" $log; then
   echo "TEST ENDED AS EXPECTED"
   exit 0
fi

# SCF, MP2 and CCSD energies:
CRIT1=`$GREP "Final * HF energy:" $log | $GREP "\-100.008919(8|9)." | wc -l`
CRIT2=`$GREP "Total SCF   energy:                  \-100.008919(8|9)." $log | wc -l`
TEST[1]=`expr $CRIT1 \+ $CRIT2`
CTRL[1]=2
ERROR[1]="ENERGIES NOT CORRECT"

# CCS freq.-dep. first hyperpolarizabilities:
CRIT1=`$GREP "ZDIPLEN  \(unrel\.\) (\-0| \-|  | 0)\.[ 0]+ ZDIPLEN  \(unrel\.\) (\-0| \-|  | 0)\.[ 0]+ ZDIPLEN  \(unrel\.\) (\-0| \-|  | 0)\.[ 0]+    16\.57326" $log | wc -l`
CRIT2=`$GREP "ZDIPLEN  \(unrel\.\) (\-0| \-|  | 0)\.[ 0]+ XDIPLEN  \(unrel\.\) (\-0| \-|  | 0)\.[ 0]+ XDIPLEN  \(unrel\.\) (\-0| \-|  | 0)\.[ 0]+   ( |0)\.675553" $log | wc -l`
CRIT3=`$GREP "XDIPLEN  \(unrel\.\) (\-0| \-|  | 0)\.[ 0]+ XDIPLEN  \(unrel\.\)  ( |0)\.[ 0]+ ZDIPLEN  \(unrel\.\) (\-0| \-|  | 0)\.[ 0]+   ( |0)\.675553" $log | wc -l`
CRIT4=`$GREP "( \-|\-0)\.0856[ ]+ ( |0)\.0428[ ]+ ( |0)\.0428      17\.01828" $log | wc -l`
CRIT5=`$GREP "( \-|\-0)\.0856[ ]+ ( |0)\.0428[ ]+ ( |0)\.0428     ( |0)\.680361" $log | wc -l`
CRIT6=`$GREP "( \-|\-0)\.0856[ ]+ ( |0)\.0428[ ]+ ( |0)\.0428     ( |0)\.732119" $log | wc -l`
TEST[2]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3 + $CRIT4 \+ $CRIT5 \+ $CRIT6`
CTRL[2]=10
ERROR[2]="CCS CARTESIAN 1. HYPERPOLARIZABILITIES NOT CORRECT"

# CCS freq.-dep. first hyperpolarizabilities:
CRIT1=`$GREP "beta_\|\| * (\-0| \-|  | 0)\.0000 * (\-0| \-|  | 0)\.0000 * (\-0| \-|  | 0)\.0000 * 10\.75462(0|1)" $log | wc -l`
CRIT2=`$GREP "( \-|\-0)\.0856   ( |0)\.0428   ( |0)\.0428     11\.068809" $log | wc -l`
CRIT3=`$GREP "beta\^K * (\-0| \-|  | 0)\.0000 * (\-0| \-|  | 0)\.0000 * (\-0| \-|  | 0)\.0000 * 10\.75462(0|1)" $log | wc -l`
CRIT4=`$GREP "( \-|\-0)\.0856   ( |0)\.0428   ( |0)\.0428     11\.12056(7|8)" $log | wc -l`
CRIT5=`$GREP "beta_\|_ * (\-0| \-|  | 0)\.0000 * (\-0| \-|  | 0)\.0000 * (\-0| \-|  | 0)\.0000 * 3\.584873." $log | wc -l`
CRIT6=`$GREP "( \-|\-0)\.0856   ( |0)\.0428   ( |0)\.0428     3\.655097." $log | wc -l`
CRIT7=`$GREP "  beta_ms  " $log | sed "-e s/D/E/" -e "s/ -/  /g" | awk '{if (\$NF < 1.0E-8) print \$0}' | wc -l`
# hjaaj: with sed take absolute value of numbers because an awk version evaluated -0.1E-15 > -1.0E-8 as false :-(
CRIT8=`$GREP "( \-|\-0)\.0856   ( |0)\.0428   ( |0)\.0428   ( \-|\-0)\.517581(6|7).(E|D)\-01" $log | wc -l`
TEST[3]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3 + $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ $CRIT7 \+ $CRIT8`
CTRL[3]=8
ERROR[3]="CCS 1. HYPERPOLARIZABILITIES AVERAGES NOT CORRECT"

# CCS freq.-dep. second hyperpolarizabilities:
CRIT1=`$GREP "ZDIPLEN (\-0| \-|  | 0)\.[ 0]* ZDIPLEN (\-0| \-|  | 0)\.[ 0]* ZDIPLEN (\-0| \-|  | 0)\.[ 0]* ZDIPLEN (\-0| \-|  | 0)\.[ 0]*   \-133\.4156" $log | wc -l`
CRIT2=`$GREP "( \-|\-0)\.0856[ ]* ( |0)\.0428[ ]* ( |0)\.0428[ ]* ( |0)\.0000     -139.2381" $log | wc -l`
CRIT3=`$GREP "( \-|\-0)\.1285[ ]* ( |0)\.0428[ ]* ( |0)\.0428[ ]* ( |0)\.0428     -145.3812" $log | wc -l`
CRIT4=`$GREP "XDIPLEN (\-0| \-|  | 0)\.[ 0]* ZDIPLEN (\-0| \-|  | 0)\.[ 0]* XDIPLEN  ( |0)\.[ 0]* ZDIPLEN (\-0| \-|  | 0)\.[ 0]*   \-5\.714267" $log | wc -l`
CRIT5=`$GREP "( \-|\-0)\.0856[ ]* ( |0)\.0428[ ]* ( |0)\.0428[ ]* ( |0)\.0000     -6.614949" $log | wc -l`
CRIT6=`$GREP "( \-|\-0)\.1285[ ]* ( |0)\.0428[ ]* ( |0)\.0428[ ]* ( |0)\.0428     -7.41759(2|3)" $log | wc -l`
CRIT7=`$GREP "ZDIPLEN (\-0| \-|  | 0)\.[ 0]* YDIPLEN (\-0| \-|  | 0)\.[ 0]* YDIPLEN  ( |0)\.[ 0]* ZDIPLEN (\-0| \-|  | 0)\.[ 0]*   \-5\.714267" $log | wc -l`
CRIT8=`$GREP "( \-|\-0)\.0856[ ]* ( |0)\.0428[ ]* ( |0)\.0428[ ]* ( |0)\.0000     -5.833282" $log | wc -l`
CRIT9=`$GREP "( \-|\-0)\.1285[ ]* ( |0)\.0428[ ]* ( |0)\.0428[ ]* ( |0)\.0428     -6.00892(8|9)" $log | wc -l`
CRIT10=`$GREP "gamma_\|\| * (\-0|\-| |0)\.0000 * (\-0|\-| |0)\.0000 * (\-0|\-| |0)\.0000 * (\-0|\-| |0)\.0000 * \-31\.3671(0|1)" $log | wc -l`
CRIT11=`$GREP "( \-|\-0)\.0856      ( |0)\.0428      ( |0)\.0428      ( |0)\.0000    \-32\.92276" $log | wc -l`
CRIT12=`$GREP "( \-|\-0)\.1285      ( |0)\.0428      ( |0)\.0428      ( |0)\.0428    \-34\.58491" $log | wc -l`
TEST[4]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3 + $CRIT4 \+ $CRIT5 \+ $CRIT6 \
           \+ $CRIT7 \+ $CRIT8 \+ $CRIT9 + $CRIT10 \+ $CRIT11 \+ $CRIT12`
CTRL[4]=26
ERROR[4]="CCS 2. HYPERPOLARIZABILITIES NOT CORRECT"

PASSED=1
for i in 1 2 3 4
do
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} ( test = ${TEST[i]}; control = ${CTRL[i]} ); "
     PASSED=0
   fi
done

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM
  exit 1
fi                
%EOF%
#######################################################################
